import java.io.*;
import java.util.*;


/**
 * Created by Administrator on 10/12/2016.
 */
public class Main {
    public static int randomNumber()    //一个用于产生正态分布随机数的函数
    {
        Random rd=new Random();
        int temp=(int)(7*rd.nextGaussian()+75);
        return temp;
    }

    public static void main(String[] args) {
        String buffer=null;
        File file=new File("list/markList.txt");
        if(file.exists())
        {
            file.delete();  //若已存在该文件，则将它删除
        }

        //用于记录所有学生每门课的总成绩
        double sumChinese=0;
        double sumMath=0;
        double sumEnglish=0;
        double sumPhysics=0;
        double sumChemistry=0;
        double sumBiology=0;

        ArrayList<Student> students = new ArrayList<Student>();     //用ArrayList类储存由许多Student类实例组成的数组
        try(
                FileReader reader = new FileReader("list/register.txt");
                BufferedReader br = new BufferedReader(reader)
                )
        {
            while((buffer=br.readLine())!=null)     //依次读取register.txt中的内容直至读取完
            {
                students.add(new Student(buffer));
            }
            for(Student student:students)   //用增强for循环给students中的每个元素的每个数据成员用随机数赋值
            {
                student.setChinese(randomNumber());
                student.setMath(randomNumber());
                student.setEnglish(randomNumber());
                student.setPhysics(randomNumber());
                student.setChemistry(randomNumber());
                student.setBiology(randomNumber());
                student.countSum();

                //该同学各门课程的成绩加到总的各门课程的总分
                sumChinese+=student.getChinese();
                sumMath+=student.getMath();
                sumEnglish+=student.getEnglish();
                sumPhysics+=student.getPhysics();
                sumChemistry+=student.getChemistry();
                sumBiology+=student.getBiology();
            }

        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }

        //给ArrayList元素按总分排序
        Collections.sort(students, new Comparator<Student>()
        {
            @Override
            public int compare(Student st1, Student st2)
            {
                if(st1.getSum() > st2.getSum())
                {
                    return -1;
                }
                else if(st1.getSum() < st2.getSum())
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
        });

        Object[] array = students.toArray();
        try(
                FileWriter writer = new FileWriter(file);
                BufferedWriter bw = new BufferedWriter(writer)
                )
        {
            bw.write("          学号           姓名      语文 数学 英语 物理 化学 生物 总分\r\n");
            for(int i=0; i<array.length;i++)
            {
                Student student2 = (Student) array[i];
                bw.write(student2.getNameAndID() + "   ");
                bw.write(student2.getChinese() + "   ");
                bw.write(student2.getMath() + "   ");
                bw.write(student2.getEnglish() + "   ");
                bw.write(student2.getPhysics() + "   ");
                bw.write(student2.getChemistry() + "   ");
                bw.write(student2.getBiology() + "   ");
                bw.write(student2.getSum() + "   \r\n");

            }
            bw.write("           课程平均分            "
                    +" "+sumChinese/array.length+" "+sumMath/array.length
                    +" "+sumEnglish/array.length+" "+sumPhysics/array.length
                    +" "+sumChemistry/array.length+" "+sumBiology/array.length
                    );
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }
    }
}
